package com.fr.data.impl;

import com.fr.base.FCloneable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary.MV;
import com.fr.data.condition.Condition;
import com.fr.data.core.DataUtils;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.script.Calculator;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class DatabaseDictionary extends FormulaDisplayDictionary
{
  protected DatabaseConnection databaseConnection;
  protected String tableName;
  protected String schema;
  private transient List kvs;

  public DatabaseDictionary()
  {
    this(new JDBCDatabaseConnection(), "", 1, 2);
  }

  public DatabaseDictionary(DatabaseConnection paramDatabaseConnection, String paramString, int paramInt1, int paramInt2)
  {
    setDatabaseConnection(paramDatabaseConnection);
    setTableName(paramString);
    setKeyColumnIndex(paramInt1);
    setValueColumnIndex(paramInt2);
  }

  public DatabaseConnection getDatabaseConnection()
  {
    return this.databaseConnection;
  }

  public void setDatabaseConnection(DatabaseConnection paramDatabaseConnection)
  {
    this.databaseConnection = paramDatabaseConnection;
    this.kvs = null;
  }

  public String getSchema()
  {
    return this.schema;
  }

  public void setSchema(String paramString)
  {
    this.schema = paramString;
    this.kvs = null;
  }

  public String getTableName()
  {
    return this.tableName;
  }

  public void setTableName(String paramString)
  {
    this.tableName = paramString;
    this.kvs = null;
  }

  public int getKeyColumnIndex()
  {
    return this.keyColumnIndex;
  }

  public void setKeyColumnIndex(int paramInt)
  {
    if (this.keyColumnIndex != paramInt)
    {
      this.keyColumnIndex = paramInt;
      this.kvs = null;
    }
  }

  public int getValueColumnIndex()
  {
    return this.valueColumnIndex;
  }

  public void setValueColumnIndex(int paramInt)
  {
    if (this.valueColumnIndex != paramInt)
    {
      this.valueColumnIndex = paramInt;
      this.kvs = null;
    }
  }

  public void reset()
  {
    this.kvs = null;
  }

  private void createKVS(Calculator paramCalculator)
  {
    if (this.kvs == null)
      if (this.databaseConnection != null)
        this.kvs = createMVList(new DBTableData(this.databaseConnection, DataUtils.createSelectSQL(this.schema, this.tableName)), paramCalculator);
      else
        this.kvs = Collections.EMPTY_LIST;
  }

  public Object get(Object paramObject, Calculator paramCalculator)
  {
    createKVS(paramCalculator);
    int i = 0;
    int j = this.kvs.size();
    while (i < j)
    {
      Dictionary.MV localMV = (Dictionary.MV)this.kvs.get(i);
      if (ComparatorUtils.equals(localMV.getModel(), paramObject))
        return localMV.getView();
      ++i;
    }
    return null;
  }

  public String[] getDependence()
  {
    return new String[0];
  }

  public Condition getCondition()
  {
    return this.condition;
  }

  public void setCondition(Condition paramCondition)
  {
    this.condition = paramCondition;
  }

  public Iterator entrys(Calculator paramCalculator)
  {
    createKVS(paramCalculator);
    return new EntryIterator(this, null);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2 = paramXMLableReader.getTagName();
      if (("DBDictAttr".equals(str2)) || ("DBMapAttr".equals(str2)) || ("JDBCMapAttr".equals(str2)))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("tableName")) != null)
          setTableName(str1);
        if ((str1 = paramXMLableReader.getAttr("schemaName")) != null)
          setSchema(str1);
        if ((str1 = paramXMLableReader.getAttr("ki")) != null)
          setKeyColumnIndex(Integer.parseInt(str1));
        if ((str1 = paramXMLableReader.getAttr("vi")) != null)
          setValueColumnIndex(Integer.parseInt(str1));
        if ((str1 = paramXMLableReader.getAttr("keyColumnIndex")) != null)
          setKeyColumnIndex(Integer.parseInt(str1) - 1);
        if ((str1 = paramXMLableReader.getAttr("valueColumnIndex")) != null)
          setValueColumnIndex(Integer.parseInt(str1) - 1);
      }
      else
      {
        Object localObject;
        if (("Connection".equals(str2)) || ("Database".equals(str2)))
        {
          localObject = DataXMLUtils.readXMLConnection(paramXMLableReader);
          if (localObject instanceof DatabaseConnection)
            setDatabaseConnection((DatabaseConnection)localObject);
        }
        else if ("JDBCDatabase".equals(str2))
        {
          localObject = new JDBCDatabaseConnection();
          paramXMLableReader.readXMLObject((XMLReadable)localObject);
          setDatabaseConnection((DatabaseConnection)localObject);
        }
        else if ("Condition".equals(str2))
        {
          localObject = DataXMLUtils.readCondition(paramXMLableReader);
          setCondition((Condition)localObject);
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("DBDictAttr").attr("tableName", getTableName()).attr("schemaName", getSchema()).attr("ki", getKeyColumnIndex()).attr("vi", getValueColumnIndex()).end();
    if (getDatabaseConnection() != null)
      DataXMLUtils.writeXMLConnection(paramXMLPrintWriter, getDatabaseConnection());
    if (this.condition != null)
      DataXMLUtils.writeXMLCondition(paramXMLPrintWriter, this.condition);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    DatabaseDictionary localDatabaseDictionary = (DatabaseDictionary)super.clone();
    if (this.databaseConnection != null)
      this.databaseConnection = ((DatabaseConnection)getDatabaseConnection().clone());
    if (this.condition != null)
      localDatabaseDictionary.condition = ((Condition)this.condition.clone());
    return localDatabaseDictionary;
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
      return true;
    if (!(paramObject instanceof DatabaseDictionary))
      return false;
    return ((super.equals(paramObject)) && (this.keyColumnIndex == ((DatabaseDictionary)paramObject).keyColumnIndex) && (this.valueColumnIndex == ((DatabaseDictionary)paramObject).valueColumnIndex) && (ComparatorUtils.equals(this.condition, ((DatabaseDictionary)paramObject).condition)));
  }

  public String toString()
  {
    return "DBDictionary" + this.databaseConnection + "[TableName:" + getTableName() + "]";
  }

  public JSONObject createJSON()
    throws Exception
  {
    JSONObject localJSONObject = new JSONObject();
    localJSONObject.put("keyIndex", getKeyColumnIndex());
    localJSONObject.put("valueIndex", getValueColumnIndex());
    localJSONObject.put("databaseName", ((NameDatabaseConnection)this.databaseConnection).getName());
    localJSONObject.put("tableName", this.tableName);
    return localJSONObject;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    if (paramJSONObject.has("keyIndex"))
      setKeyColumnIndex(paramJSONObject.getInt("keyIndex"));
    if (paramJSONObject.has("valueIndex"))
      setValueColumnIndex(paramJSONObject.getInt("valueIndex"));
    if (paramJSONObject.has("databaseName"))
      setDatabaseConnection(new NameDatabaseConnection(paramJSONObject.getString("databaseName")));
    if (paramJSONObject.has("tableName"))
      setTableName(paramJSONObject.getString("tableName"));
  }

  static List access$100(DatabaseDictionary paramDatabaseDictionary)
  {
    return paramDatabaseDictionary.kvs;
  }

  private class EntryIterator
    implements Iterator
  {
    private int next;
    private final DatabaseDictionary this$0;

    private EntryIterator()
    {
      this.this$0 = paramDatabaseDictionary;
      this.next = 0;
    }

    public boolean hasNext()
    {
      return (this.next < DatabaseDictionary.access$100(this.this$0).size());
    }

    public Object next()
    {
      Object localObject = DatabaseDictionary.access$100(this.this$0).get(this.next);
      this.next += 1;
      return localObject;
    }

    public void remove()
    {
    }

    EntryIterator(, DatabaseDictionary.1 param1)
    {
      this(paramDatabaseDictionary);
    }
  }
}